package com.example.graduatetest.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.graduatetest.R;
import com.example.graduatetest.bean.Student;

import java.util.List;

public class StudentItemAdapter extends RecyclerView.Adapter<StudentItemAdapter.ViewHolder> {
    private List<Student> studentList;
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_name_first;
        TextView tv_name_all;
        TextView tv_major;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_name_first = (TextView)itemView.findViewById(R.id.tv_name_first);
            tv_name_all = (TextView)itemView.findViewById(R.id.tv_name_all);
            tv_major = (TextView)itemView.findViewById(R.id.tv_major);
        }
    }
    public StudentItemAdapter(List<Student> studentList) {
        this.studentList = studentList;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.student_item, parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
          Student student = studentList.get(position);
          holder.tv_name_first.setText(student.getName().substring(0, 1));
          holder.tv_name_all.setText(student.getName());
          holder.tv_major.setText(student.getMajor());
    }

    @Override
    public int getItemCount() {
        return studentList.size();
    }
}
